<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" class="demo-form-inline filter-container">
      <el-form-item prop="dateType" label="时间：">
        <el-date-picker
          v-model="form.time"
          class="filter-item"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd H:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="代理账号：">
        <el-input v-model="form.userName"></el-input>
      </el-form-item>

      <el-form-item>
        <el-col :span="12">
          <el-form-item prop="filterMin">
            <el-input v-model.number="form.filterMin">
              <template slot="prepend">
                <el-select v-model="form.filter" style="width: 120px;">
                  <el-option
                    v-for="item in consts.ORDER_FIELD"
                    :value="item.key"
                    :label="item.title"
                    :key="item.value"
                  ></el-option>
                </el-select>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="1" style="text-align: center">
          -
        </el-col>
        <el-col :span="6">
          <el-form-item prop="filterMax">
            <el-input v-model.number="form.filterMax"></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item v-if="roleCheck('virtual')" class="with-pre-select" label="数据类型">
        <el-select v-model="form.isFake">
          <el-option v-for="item in consts.STATISTICS_RANGE"
                     :key="item.value"
                     :label="item.title"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-button class="filter-item" type="primary" icon="el-icon-download" @click="exportExcel">导出</el-button>
      <el-button type="primary" v-waves icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
    </el-form>

    <el-table :data="tableData.list"
              v-loading="loading"
              element-loading-text="给我一点时间"
              border fit
              highlight-current-row
              style="width: 100%"
              :header-cell-class-name="setHeaderStyle"
              :cell-class-name="setCellStyle"
    >
      <el-table-column label="代理账户名" align="center">
        <template slot-scope="{row}">
          <el-tag class="pointer" @click.native="toggleAgency(row)">
            {{row.userName}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="注册人数" align="center" prop="registerNum" sortable>
      </el-table-column>
      <el-table-column label="存款人数" align="center" prop="rechargeNum" sortable>
      </el-table-column>
      <el-table-column label="取款人数" align="center" prop="withdrawNum" sortable>
      </el-table-column>
      <el-table-column label="存款金额" align="center" width="150" prop="rechargeSum" sortable>
        <template slot-scope="{row}">
          {{row.rechargeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="取款金额" align="center" width="150" prop="withdrawSum" sortable>
        <template slot-scope="{row}">
          {{row.withdrawSum | currency}}
        </template>
      </el-table-column>

      <!--盈棋牌亏重新计算后的字段-->
      <el-table-column label="公司" align="center">
        <el-table-column label="有效投注额" align="center" width="130" prop="betEffectSum" sortable>
          <template slot-scope="{row}">
            <el-button type="text" @click="checkAgency('/report/report-game',row)">{{row.betEffectSum | currency}}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="总投注额" align="center" width="130" prop="betSum" sortable>
          <template slot-scope="{row}">{{row.betSum | currency}}</template>
        </el-table-column>
        <el-table-column label="派彩额" align="center" width="130" prop="prizeSum" sortable>
          <template slot-scope="{row}">{{row.prizeSum | currency}}</template>
        </el-table-column>
        <el-table-column label="公司输赢" align="center" width="130" prop="winSum" sortable>
          <template slot-scope="{row}">
            {{row.winSum | currency}}
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="玩家对战类" align="center">
        <el-table-column label="总投注额" align="center" width="130" prop="playerBetSum" sortable>
          <template slot-scope="{row}">{{row.playerBetSum | currency}}</template>
        </el-table-column>
        <el-table-column label="输赢" align="center" width="130" prop="playerWinSum" sortable>
          <template slot-scope="{row}">{{row.playerWinSum | currency}}</template>
        </el-table-column>
        <el-table-column label="公司抽水" align="center" width="130" prop="playerWaterFee" sortable>
          <template slot-scope="{row}">{{row.playerWaterFee | currency}}</template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="公司总盈利" align="center" width="130" prop="totalWin" sortable>
        <template slot-scope="{row}">{{row.totalWin | currency}}</template>
      </el-table-column>
      <!--结束-->
      <!--新版本不需要的-->
      <!-- <el-table-column label="总投注额" align="center" width="150" prop="withdrawSum" sortable>
        <template slot-scope="{row}">
          {{row.bettingTotalSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="有效投注额" align="center" width="150" prop="bettingSum" sortable>
        <template slot-scope="{row}">
          <el-button type="text" @click="checkAgency('/report/report-game',row)">{{row.bettingSum |
            currency}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="派彩额" align="center" width="150" prop="prizeSum" sortable>
        <template slot-scope="{row}">
          {{row.prizeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="公司输赢" align="center" width="150" prop="winSum" sortable>
        <template slot-scope="{row}">
          {{-1 * row.winSum | currency}}
        </template>
      </el-table-column> -->
      <el-table-column label="红利（包括加币-计入红利）" align="center" width="150" prop="bonusSum" sortable>
        <template slot-scope="{row}">
          <!-- <el-button type="text" @click="checkAgency('/activity/activity-fund',row)">{{row.bonusSum |
            currency}}
          </el-button> -->
          {{row.bonusSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="加币（不计红利）" align="center" width="150" prop="addCoinSum" sortable>
        <template slot-scope="{row}">{{row.addCoinSum | currency}}</template>
      </el-table-column>
      <el-table-column label="减币" align="center" width="150" prop="subCoinSum" sortable>
        <template slot-scope="{row}">{{row.subCoinSum | currency}}</template>
      </el-table-column>
      <!-- <el-table-column label="红利人数" align="center" width="150" prop="bonusSum">
      </el-table-column> -->


      <el-table-column label="返水" align="center" width="150" prop="rebateSum" sortable>
        <template slot-scope="{row}">
          <el-button type="text" @click="checkAgency('/activity-folder/activity-rebate',row)">{{row.rebateSum |
            currency}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <custom-dialog :visible="dialogVisible" returnButtonName="返回" :callback="closeDialog">
      <div slot="title">&nbsp;</div>
      <sub-list v-if="dialogVisible" :row-data="dialogProps"></sub-list>
    </custom-dialog>
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  form, grid, dialog, handleDownload,
} from '@/build';

import SubList from './subList';

import {
  reportAgencyListApi as listApi,
} from '@/services/api/report';

export default {
  name: 'report-agency',

  mixins: [form, grid, dialog, handleDownload],

  components: {
    SubList,
  },

  data() {
    const monthTime = [moment()
      .startOf('month')
      .format('YYYY-MM-DD H:mm:ss'), moment()
      .format('YYYY-MM-DD H:mm:ss')];
    return {
      tableData: [],
      form: {
        time: monthTime,
        userName: undefined,
        filterMin: undefined,
        filterMax: undefined,
        filter: undefined,
        isFake: 0,
      },
      filters: {
        stime: moment(monthTime[0])
          .valueOf(),
        etime: moment(monthTime[1])
          .valueOf(),
        export: undefined,
        isFake: 0,
        page: 1,
        size: 20,
      },
    };
  },
  methods: {
    setHeaderStyle({
      row, column, rowIndex, columnIndex,
    }) {
      if ([6].includes(columnIndex) && rowIndex == 0) {
        return 'header-green';
      }
      if ([0, 1, 2, 3].includes(columnIndex) && rowIndex == 1) {
        return 'header-green';
      }
      if ([7].includes(columnIndex) && rowIndex == 0) {
        return 'header-yellow';
      }
      if ([4, 5, 6, 7].includes(columnIndex) && rowIndex == 1) {
        return 'header-yellow';
      }
      if ([6, 8].includes(columnIndex)) {
        return 'border-left';
      }
    },
    setCellStyle({
      row, column, rowIndex, columnIndex,
    }) {
      if ([6, 7, 8, 9].includes(columnIndex)) {
        return 'header-green';
      }
      if ([10, 11, 12].includes(columnIndex)) {
        return 'header-yellow';
      }
      if ([13].includes(columnIndex)) {
        return 'border-left';
      }
    },
    submitFilterForm() {
      const {
        time, userName, filterMin, filterMax, filter, isFake,
      } = this.form;
      let stime;
      let
        etime;
      if (!_.isUndefined(time) && _.isArray(time)) {
        stime = moment(time[0])
          .valueOf();
        etime = moment(time[1])
          .valueOf();
      }

      this.filters = {
        ...this.filters,
        filterMin,
        filterMax,
        filter,
        userName,
        stime,
        etime,
        isFake,
      };
      this.getAll();
    },

    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价';
          return;
        }
        const values = data.map((item) => {
          if (column.property === 'winSum') {
            // return Number(item[column.property]) * -1;
            return Number(item[column.property]);
          }
          return Number(item[column.property]);
        });
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            }
            return prev;
          }, 0);
        }
      });

      return sums;
    },

    sortChange(params) {
      const { prop, order } = params;
      const orderField = _.find(consts.ORDER_FIELD, o => o.key === prop);
      this.filters = {
        ...this.filters,
        orderType: order == 'ascending' ? 0 : 1,
        orderField: orderField.value,
      };
      this.getAll();
    },
    toggleAgency(row) {
      this.openDialog({
        props: { ...row, ...this.form },
        dialogName: 'custom-dialog',
      });
    },
    checkAgency(path, data) {
      this.$router.push({
        path,
        query: {
          username: data.userName,
          time: this.form.time && this.form.time.length == 2 ? [`${this.form.time[0]}`, `${this.form.time[1]}`] : [],
        },
      });
    },
  },

  mounted() {
    this.bindGet(listApi, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
    });

    this.getAll();
  },
};
</script>

<style lang="scss" scoped>
  .underline-link {
    text-decoration: underline;
    color: blue;
  }
</style>
<style>
  .header-green {
    background: rgb(235, 235, 255) !important;
    color: #333;
    border: 1px solid #aaa;
  }

  .header-yellow {
    background: rgb(255, 235, 235) !important;
    color: #333;
    border: 1px solid #aaa;
  }

  .border-left {
    border-left: 1px solid #aaa;
  }
</style>
